package models;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.PrimaryKeyJoinColumn;


import play.db.jpa.Model;

@Entity
@PrimaryKeyJoinColumn(name="id", referencedColumnName="id")
public class DriverJourney extends Journey {
	
	@ManyToOne
	private Vehicle vehicle;
	
	private String expecteddistance;
	private String expectedtime;
	
	private Integer numberofhitchhiker;
	
	@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
    @JoinColumn(name="PASS")
	private List<User> passengers;
	
	@OneToMany(mappedBy = "journey", cascade = CascadeType.ALL)
	private List<Request> requests;
	
	public DriverJourney(User user, Vehicle vehicle, String departurestate,
			String departuretownn, String departurestreet, Date departuredate,
			String departurehour, String arrivestate,
			String arrivetownn, String arrivestreet, String journeynotes,
			String expecteddistance, String expectedtime, boolean priceshare,
			Integer numberofhitchhiker, List<User> passengers, List<Request> requests) {
		super(user, departurestate, departuretownn, departurestreet, 
				departuredate, departurehour, arrivestate, arrivetownn, arrivestreet, 
				journeynotes, priceshare);
		this.vehicle = vehicle;
		this.expecteddistance = expecteddistance;
		this.expectedtime = expectedtime;
		this.numberofhitchhiker = numberofhitchhiker;
		this.passengers = new ArrayList<User>();
		this.requests = new ArrayList<Request>();
	}
	
	public Vehicle getVehicle() {
		return vehicle;
	}
	public void setVehicle(Vehicle vehicle) {
		this.vehicle = vehicle;
	}
	
	public String getExpecteddistance() {
		return expecteddistance;
	}
	public void setExpecteddistance(String expecteddistance) {
		this.expecteddistance = expecteddistance;
	}
	public String getExpectedtime() {
		return expectedtime;
	}
	public void setExpectedtime(String expectedtime) {
		this.expectedtime = expectedtime;
	}
	
	public Integer getNumberofhitchhiker() {
		return numberofhitchhiker;
	}
	public void setNumberofhitchhiker(Integer numberofhitchhiker) {
		this.numberofhitchhiker = numberofhitchhiker;
	}
	
	
	public List<User> getPassengers() {
		return passengers;
	}

	public void setPassengers(List<User> passengers) {
		this.passengers = passengers;
	}

	public List<Request> getRequests() {
		return requests;
	}

	public void setRequests(List<Request> requests) {
		this.requests = requests;
	}

	public static List<DriverJourney> getAllDriverJourney(){
		return DriverJourney.findAll();	
	}
	
	public static List<DriverJourney> orderDriverJourney(){
		return (DriverJourney.find("order by id desc").fetch());
	}
	
	public static List<DriverJourney> searchDriverJourney(String departurestate, String arrivestate) {

		if(departurestate != null) {
			departurestate = "%" + departurestate.toString() + "%";
			departurestate = departurestate.toLowerCase();
		}
		
		if(arrivestate != null) {
			arrivestate = "%" + arrivestate.toString() + "%";
			arrivestate = arrivestate.toLowerCase();
		}

		return (DriverJourney.find("LOWER(departurestate) like ? AND LOWER(arrivestate) like ? order by id desc", departurestate, arrivestate).fetch());
	}
	
	public static List<DriverJourney> filterDriverJourney(String departurestate, String departuretownn,	String departurestreet, String arrivestate, String arrivetownn, String arrivestreet) {
		if(departurestate != null) {
			departurestate = "%" + departurestate.toString() + "%";
			departurestate = departurestate.toLowerCase();
		}
		
		if(departuretownn != null) {
			departuretownn = "%" + departuretownn.toString() + "%";
			departuretownn = departuretownn.toLowerCase();
		}
		
		if(departurestreet != null) {
			departurestreet = "%" + departurestreet.toString() + "%";
			departurestreet = departurestreet.toLowerCase();
		}
		
		if(arrivestate != null) {
			arrivestate = "%" + arrivestate.toString() + "%";
			arrivestate = arrivestate.toLowerCase();
		}
		
		if(arrivetownn != null) {
			arrivetownn = "%" + arrivetownn.toString() + "%";
			arrivetownn = arrivetownn.toLowerCase();
		}
		
		if(arrivestreet != null) {
			arrivestreet = "%" + arrivestreet.toString() + "%";
			arrivestreet = arrivestreet.toLowerCase();
		}

		return (DriverJourney.find("LOWER(departurestate) like ? AND LOWER(departuretownn) like ? AND LOWER(departurestreet) like ? AND LOWER(arrivestate) like ? AND LOWER(arrivetownn) like ? AND LOWER(arrivestreet) like ? order by id desc", departurestate, departuretownn, departurestreet, arrivestate, arrivetownn, arrivestreet).fetch());
	}
	
	public void updateDriver(String departurestate,
			String departuretownn, String departurestreet, Date departuredate,
			String departurehour, String arrivestate,
			String arrivetownn, String arrivestreet, String journeynotes,
			String expecteddistance, String expectedtime, boolean priceshare,
			Integer numberofhitchhiker){
		
			this.setDeparturestate(departurestate);
			this.setDeparturedate(departuredate);
			this.setDeparturehour(departurehour);
			this.setArrivestate(arrivestate);
			this.setArrivetownn(arrivetownn);
			this.setArrivestreet(arrivestreet);
			this.setJourneynotes(journeynotes);
			this.setExpecteddistance(expecteddistance);
			this.setExpectedtime(expectedtime);
			this.setPriceshare(priceshare);
			this.setNumberofhitchhiker(numberofhitchhiker);
			this.save();
		
	}
}
